package com.bbs.controller;

import com.bbs.entity.ResultInfo;
import com.bbs.entity.User;
import com.bbs.service.PostService;
import com.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;
    /**
     * 去注册和登录的页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 用户注册
     * @param user 接收前端上送的参数-json
     * @return 返回json格式的数据给前端
     */
    @RequestMapping(value = "/register")
    @ResponseBody
    public ResultInfo register(@RequestBody User user){
        ResultInfo resultInfo = new ResultInfo();
        int flag = userService.register(user);
        if(flag == 1){
            resultInfo.setCode("0");
            resultInfo.setMsg("注册成功，请激活后登录");
        }else if(flag == 2){
            resultInfo.setCode("2");
            resultInfo.setMsg("邮箱已被占用");
        }else {
            resultInfo.setCode("1");
            resultInfo.setMsg("注册失败，请重新注册");
        }
        return resultInfo;
    }

    /**
     * 激活
     * @param code
     * @return
     */
    @RequestMapping(value = "/activate")
    @ResponseBody
    public String activate(String code){
        userService.active(code);
        return "激活成功，请登录";
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public ResultInfo doLogin(@RequestBody User user, HttpSession session){
        System.out.println(user.getEmail() + "--"+user.getPassword());
        // 调用service方法校验邮箱和密码
        User exitUser = userService.checkPassword(user);
        ResultInfo resultInfo = new ResultInfo();
        if(Objects.nonNull(exitUser) && exitUser.getId() != 0 && exitUser.getActived() != 0){
            session.setAttribute("uid",exitUser.getId());// 登录成功把个人信息存入会话
            session.setAttribute("headUrl",exitUser.getHeadUrl());// 登录成功把个人信息存入会话
            resultInfo.setCode("0");
            resultInfo.setMsg("登录成功");
        }else {
            resultInfo.setCode("1");
            resultInfo.setMsg("登录失败");
        }
        return resultInfo;
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //session.removeAttribute("uid");
        session.invalidate();// 销毁session
        return "login";
    }

    @RequestMapping(value="/test")
    @ResponseBody
    public ResultInfo save12(@RequestBody User user) throws IOException {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode("0");
        resultInfo.setMsg("注册成功，请激活后登录");
        System.out.println(user);
        return resultInfo;
    }

}
